<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@ include file="../common/verify.jsp"%>
<jsp:include page="../common/meta.jsp"></jsp:include>
<jsp:include page="../common/easyui.jsp"></jsp:include>
<script type="text/javascript">
	var urlClassTree;
	var urlClassMenu;
	var editUrlClassWindow;
	var editUrlClassForm;
	$(function() {
		editUrlClassWindow = $('#editUrlClassWindow').window({
			closed : true
		});

		editUrlClassForm = editUrlClassWindow.find('form');

		// 加载网址分类列表选项
		$('#parentID').combobox({
			url : '/com.sundaying.gooway/admin/listUrlClassJson',
			valueField : 'classID',
			textField : 'className'
		});

		$('#btnSaveUrlClass').linkbutton();
		$('#btnCloseUrlClassWindow').linkbutton();

		urlClassMenu = $('#urlClassMenu').menu({
			onClick : function(item) {
				console.info('点击菜单：' + item.text);
				if (item.text == '删除') {
					removeUrlClass();
				} else if (item.text == '修改') {
					editUrlClass();
				} else if (item.text == '新增') {
					newUrlClass();
				} else if (item.text == '展开') {
					expandAll();
				}
			}
		});

		urlClassTree = $('#urlClassTree').tree({
			url : '/com.sundaying.gooway/admin/treeUrlClassJson',
			animate : false,
			checkbox : false,
			onlyLeafCheck : false,
			lines : true,
			dnd : false,
			onSelect : function(node) {
				console.info('当前选中：' + node.id);
				getChecked();
			},
			onContextMenu : function(e, node) {
				e.preventDefault();
				urlClassTree.tree('select', node.target);
				urlClassMenu.menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}
		});

	});

	function getChecked() {
		var nodes = urlClassTree.tree('getChecked');
		var s = '';
		for ( var i = 0; i < nodes.length; i++) {
			if (s != '')
				s += ',';

			s += nodes[i].text;
		}
		console.info('已选择：' + s);
	}

	function expandAll() {
		urlClassTree.tree('expandAll');
	}

	function newUrlClass() {
		console.info('增加...');
		var node = urlClassTree.tree('getSelected');
		if (node) {
			editUrlClassWindow.window('open');
			editUrlClassForm.form('clear');
			editUrlClassWindow.find('#classID').attr('readonly', false);
			editUrlClassWindow.find('#parentID').combobox('setValue', node.id);
			editUrlClassForm.url = '/com.sundaying.gooway/admin/addUrlClassJson';
		}
	}

	function removeUrlClass() {
		var node = urlClassTree.tree('getSelected');
		var childNode = urlClassTree.tree('getChildren', node.target);
		console.info('有了节点数量：' + childNode.length);
		if (childNode.length > 0) {
			$.messager.alert('提示', '不能删除有子分类的分类！', 'error');
			return;
		}
		if (node) {
			$.messager.confirm('请确认', '您要删除当前选择的项目？', function(r) {
				if (r) {
					$.ajax({
						url : '/com.sundaying.gooway/admin/deleteUrlClassJson',
						data : {
							classID : node.id
						},
						cache : false,
						dataType : "json",
						success : function(response) {
							//			 				urlClassTree.tree('remove', node.target);
							urlClassTree.tree('reload');
							expandAll();
							$.messager.show({
								title : '提示',
								msg : '删除成功！'
							});
						}
					});
				}
			});
		}

	}

	function editUrlClass() {
		console.info('修改...');
		var node = urlClassTree.tree('getSelected');
		var parentNode = urlClassTree.tree('getParent', node.target);
		if (node) {
			editUrlClassWindow.window('open');
			editUrlClassWindow.find('#classID').attr('readonly', true);
			editUrlClassWindow.find('#classID').val(node.id);
			editUrlClassWindow.find('#className').val(node.text);
			editUrlClassWindow.find('#parentID').combobox('setValue',
					parentNode.id);
			editUrlClassForm.url = '/com.sundaying.gooway/admin/updateUrlClassJson';
		} else {
			$.messager.alert('提示', '请选择要修改的记录！', 'error');
		}
	}

	function saveUrlClass() {
		console.info('保存...');
		if (editUrlClassForm.url == '') {
			editUrlClassForm.url = '/com.sundaying.gooway/admin/addUrlClassJson';
		}

		editUrlClassForm.form('submit', {
			url : editUrlClassForm.url,
			success : function(data) {
				console.info(data);
				editUrlClassWindow.window('close');
				urlClassTree.tree('reload');
				$.messager.show({
					title : '提示',
					msg : '保存成功！'
				});
			},
			error : function(json) {
				console.info('保存出错！');
			}
		});
	}

	function closeUrlClassWindow() {
		editUrlClassWindow.window('close');
	}
</script>
<title>网址分类列表</title>
</head>
<body style="width: 720px; height: 520px; margin: 0 auto;">

	<div title="网址分类" style="width: 100%; height: 100%;">
		<ul id="urlClassTree"></ul>
	</div>

	<div id="urlClassMenu" class="easyui-menu" style="width: 120px;">
		<div>新增</div>
		<div class="menu-sep"></div>
		<div>删除</div>
		<div class="menu-sep"></div>
		<div>修改</div>
		<div class="menu-sep"></div>
		<div>展开</div>
	</div>

	<div id="editUrlClassWindow" title="网址分类"
		style="width: 400px; height: 250px;">
		<div style="padding: 20px 20px 40px 80px;">
			<form method="post">
				<table>
					<tr>
						<td>类别编码：</td>
						<td><input id="classID" name="urlClass.classID"></input></td>
					</tr>
					<tr>
						<td>分类名称：</td>
						<td><input id="className" name="urlClass.className"></input></td>
					</tr>
					<tr>
						<td>上级分类：</td>
						<td><input id="parentID" name="urlClass.parentID"></input></td>
					</tr>
				</table>
			</form>
		</div>
		<div style="text-align: center; padding: 5px;">
			<a href="javascript:void(0)" onclick="saveUrlClass()"
				id="btnSaveUrlClass" icon="icon-save">保存</a> <a
				href="javascript:void(0)" onclick="closeUrlClassWindow()"
				id="btnCloseUrlClassWindow" icon="icon-cancel">取消</a>
		</div>
	</div>
</body>
</html>